local skel = fk.CreateSkill {
  name = "rmt__kuizhul",
}

Fk:loadTranslationTable{
  ["rmt__kuizhul"] = "揆助",
  [":rmt__kuizhul"] = "其他角色的准备阶段，你可以与其拼点，赢的角色选择将此阶段改为摸牌或弃牌阶段，若两张拼点牌花色相同，也可以选择改为"..
  "拥有“乱击”的出牌阶段。",

  ["#rmt__kuizhul-invoke"] = "揆助：是否与 %dest 拼点？赢者可以将此准备阶段改为另一个阶段",
  ["#rmt__kuizhul-choice"] = "揆助：将 %dest 此准备阶段改为另一个阶段",
  ["rmt__kuizhul_luanji"] = "出牌阶段，且拥有“乱击”",
  ["@@rmt__kuizhul-phase"] = "乱击",
}


skel:addEffect(fk.EventPhaseChanging, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.phase == Player.Start and not target.dead and target ~= player and
      player:canPindian(target) and not data.skipped
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#rmt__kuizhul-invoke::"..target.id }) then
      event:setCostData(self, { tos = {target} } )
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pindian = player:pindian({target}, skel.name)
    local result = pindian.results[target]
    if not result then return end
    local winner = result.winner
    if winner and not winner.dead then
      local choices = {"phase_draw", "phase_discard"}
      if pindian.fromCard and result.toCard and pindian.fromCard:compareSuitWith(result.toCard) then
        table.insert(choices, "rmt__kuizhul_luanji")
      end
      local choice = room:askToChoice(winner, {
        choices = choices, skill_name = skel.name, prompt = "#rmt__kuizhul-choice::"..target.id,
      })
      if choice == "phase_draw" then
        data.phase = Player.Draw
      elseif choice == "phase_discard" then
        data.phase = Player.Discard
      elseif choice == "rmt__kuizhul_luanji" then
        data.phase = Player.Play
        if not target.dead then
          data.extra_data = data.extra_data or {}
          data.extra_data.rmt__kuizhul_luanji = true
        end
      end
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function (self, event, target, player, data)
    if target == player and player.phase == Player.Play then
      return data.extra_data and data.extra_data.rmt__kuizhul_luanji
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@rmt__kuizhul-phase", 1) -- 仅做宝宝标记
    if not player:hasSkill("luanjirmt__luanji", true) then
      room:handleAddLoseSkills(player, "rmt__luanji", nil, true, false)
      local parent = room.logic:getCurrentEvent():findParent(GameEvent.Phase, true)
      if parent then
        parent:addCleaner(function()
          room:handleAddLoseSkills(player, "-rmt__luanji")
        end)
      end
    end
  end,
})

return skel
